package cmbchina.util.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by zm on 17-11-12.
 */
public class FlowDb {

    /**
     * 修改流程状态
     * @param state 状态
     * @param process_inst_id 流程实例id
     * @return
     */
    public static int updateFlowState(int state,String process_inst_id) {
        DBHelper dbHelper = new DBHelper();
        Connection conn = dbHelper.getConn();
        int i = 0;
        String sql = "update cus_flow  set state = ?  where process_inst_id = ? ";
        PreparedStatement pstmt = null;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            pstmt.setInt(1,state);
            pstmt.setString(2,process_inst_id);
            i = pstmt.executeUpdate();
            System.out.println("update cus_flow  set state = ?  where process_inst_id = ?"+state+""+process_inst_id );

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            dbHelper.close(conn,pstmt,null);
        }
        return i;
    }

    public static int queryFormState(String task_definition_key,String process_inst_id) {
        DBHelper dbHelper = new DBHelper();
        Connection conn = dbHelper.getConn();
        int i = 0;
        int state = 0;
        ResultSet ret = null;
        String sql = "select state from cus_flow  where process_inst_id = ? ";
        PreparedStatement pstmt = null;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            pstmt.setString(1,process_inst_id);
            ret = pstmt.executeQuery();
            while (ret.next()) {
                state = ret.getInt(1);
            }
            System.out.println("select state from cus_flow  where process_inst_id = ? "+process_inst_id );

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            dbHelper.close(conn,pstmt,ret);
        }
        return 0;
    }

    public static Map<String,String> queryCreateUserId(String process_inst_id) {
        DBHelper dbHelper = new DBHelper();
        Connection conn = dbHelper.getConn();
        Map<String,String> resultMap = new LinkedHashMap<>();
        int i = 0;
        String createUserId = "";
        ResultSet ret = null;
        String sql = "select create_user_id,create_user_name from cus_flow where process_inst_id= ? ";
        PreparedStatement pstmt = null;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            pstmt.setString(1,process_inst_id);
            ret = pstmt.executeQuery();
            while (ret.next()) {
//                createUserId = ret.getString(1);
                resultMap.put("userId",ret.getString(1));
                resultMap.put("userName",ret.getString(2));
            }
            System.out.println("select createUserId from cus_flow  where process_inst_id = ? "+process_inst_id );

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            dbHelper.close(conn,pstmt,ret);
        }
        return resultMap;
    }
}
